<?php

namespace Classes;

use Classes\db\DBConnection;
use Classes\Relation;
use PDO;
use Exception;

class Event {
	public static function searchEvent($event_name) {
		$connection = DBConnection::getDBConnection ();
		
		$event_name = "%" . $event_name . "%";
		
		$statement = "SELECT *
						FROM `event`
						WHERE event_name like :EVENT_NAME
						ORDER BY date desc";
		$stmt = $connection->prepare ( $statement );
		$stmt->bindValue ( ":EVENT_NAME", $event_name, \PDO::PARAM_STR );
		$stmt->execute ();
		$result = $stmt->fetchAll ();
		return $result;
	}
	public static function insertEvent($event_name, $date, $author, $description, $place) {
		$connection = DBConnection::getDBConnection ();
		
		$statement = "INSERT INTO `event`(`event_name`, `date`, `author_id`, `description`, `place`) 
				VALUES (:EVENT_NAME,:DATE,:AUTHOR,:DESCRIPTION,:PLACE)";
		
		$stmt = $connection->prepare ( $statement );
		$stmt->bindValue ( ":EVENT_NAME", $event_name, \PDO::PARAM_STR );
		$stmt->bindValue ( ":DATE", $date, \PDO::PARAM_STR );
		$stmt->bindValue ( ":AUTHOR", $author, \PDO::PARAM_INT );
		$stmt->bindValue ( ":DESCRIPTION", $description, \PDO::PARAM_STR );
		$stmt->bindValue ( ":PLACE", $place, \PDO::PARAM_STR );
		$stmt->execute ();
	}
	public static function deleteEvent($event_id) {
		$connection = DBConnection::getDBConnection ();
		
		$statement = "DELETE FROM `event` WHERE event_id=:EVENT_ID";
		
		$stmt = $connection->prepare ( $statement );
		$stmt->bindValue ( ":EVENT_ID", $event_id, \PDO::PARAM_INT );
		$stmt->execute ();
	}
	public static function getEvent($event_id) {
		$connection = DBConnection::getDBConnection ();
		
		$statement = "SELECT event.event_name,
							 event.description,
							 event.date,
							 event.place,
							 event.author_id,
							 user.user_name
						FROM `event`
						INNER JOIN `user`
						WHERE event_id=:EVENT_ID and user.user_id=event.author_id";
		
		$stmt = $connection->prepare ( $statement );
		$stmt->bindValue ( ":EVENT_ID", $event_id, \PDO::PARAM_INT );
		$stmt->execute ();
		
		$result = $stmt->fetchAll ();
		return $result;
	}
	public static function getEventsByUserId($user_id) {
		$connection = DBConnection::getDBConnection ();
		
		$statement = "SELECT event.event_name,
							 event.description,
							 event.date,
							 event.place,
							 event.event_id,
							 user.user_name
						FROM `event`
						INNER JOIN `user`
						WHERE user.user_id=event.author_id and user.user_id=:USER_ID";
		
		$stmt = $connection->prepare ( $statement );
		$stmt->bindValue ( ":USER_ID", $user_id, \PDO::PARAM_INT );
		$stmt->execute ();
		
		$result = $stmt->fetchAll();
		return $result;
	}
	
	
}